#include "gameMain.h"

#include "consoleFns.h"

#include "gameSystem.h"

#include "sysConsole.h"
#include "sysEditor.h"
#include "sysGameLoop.h"
#include "sysGUI.h"

#include "debugFns.h"
#include "debugVars.h"

#include "glut.h"

bool gGameMain_ShowFps;

// Generic debug font that should be used whenever you don't care what it looks like
ofTrueTypeFont 	gGameMain_debugFont;

float gMouseX;
float gMouseY;

//--------------------------------------------------------------------------------------------------
void gameMain::setup()
{
	ofSetFrameRate(60);
	ofSetLogLevel(OF_LOG_VERBOSE);
	ofBackground(34, 34, 34);
	ofSetVerticalSync(true);
	ofEnableAlphaBlending();
	ofLogToFile("log.txt", false);
	ofSetEscapeQuitsApp(false);

	gGameMain_debugFont.loadFont("vag.ttf", 10);
	consoleFns_register();

	// GUI
	gameSystem_register(sysGUI_getSystem());

	// Register all of the game systems here before setup is called
	gameSystem_register(sysConsole_getSystem());

	// New racing game
	gameSystem_register(sysGameLoop_getSystem());

	// Level editor
	gameSystem_register(sysEditor_getSystem());

	gameSystem_setupAll();

	sysConsole_logMessage("All systems initialized.");
}

//--------------------------------------------------------------------------------------------------
void gameMain::update()
{
	// Update mouse globals
	gMouseX = mouseX;
	gMouseY = mouseY;

	gameSystem_updateAll();
}

//--------------------------------------------------------------------------------------------------
void gameMain::draw()
{
	gameSystem_drawAll();

	ofSetColor(255);
	// Show fps
	if(gGameMain_ShowFps)
		gGameMain_debugFont.drawString(ofToString(ofGetFrameRate()), ofGetWidth() - 64, ofGetHeight() - 32);
}

//--------------------------------------------------------------------------------------------------
void gameMain::keyPressed(int key)
{
	// Alt-F4 (quit)
	if(key == OF_KEY_F4 && glutGetModifiers() & GLUT_ACTIVE_ALT)
		ofExit();
	// Alt-Enter (toggle fullscreen)
	else if(key == OF_KEY_RETURN && glutGetModifiers() & GLUT_ACTIVE_ALT)
		ofToggleFullscreen();
	// if the console is open, this should be the only thing that gets key input
	else if(sysConsole_hasFocus())
		sysConsole_getSystem()->keyPressed(key);
	else
		gameSystem_keyPressed(key);
}

//--------------------------------------------------------------------------------------------------
void gameMain::keyReleased(int key)
{
	// if the console is open, this should be the only thing that gets key input
	if(sysConsole_hasFocus())
		sysConsole_getSystem()->keyReleased(key);
	else
		gameSystem_keyReleased(key);
}

//--------------------------------------------------------------------------------------------------
void gameMain::mouseMoved(int x, int y )
{
	gameSystem_mouseMoved(x,y);
}

//--------------------------------------------------------------------------------------------------
void gameMain::mouseDragged(int x, int y, int button)
{
	gameSystem_mouseDragged(x,y,button);
}

//--------------------------------------------------------------------------------------------------
void gameMain::mousePressed(int x, int y, int button)
{
	gameSystem_mousePressed(x,y,button);
}

//--------------------------------------------------------------------------------------------------
void gameMain::mouseReleased(int x, int y, int button)
{
	gameSystem_mouseReleased(x,y,button);
}

//--------------------------------------------------------------------------------------------------
void gameMain::windowResized(int w, int h)
{
	gameSystem_windowResized(w,h);
}

//--------------------------------------------------------------------------------------------------
void gameMain::gotMessage(ofMessage msg)
{

}

//--------------------------------------------------------------------------------------------------
void gameMain::dragEvent(ofDragInfo dragInfo)
{

}

//--------------------------------------------------------------------------------------------------
void gameMain::exit()
{
	gameSystem_exitAll();
	debugVar_destroy();
	debugFns_destroy();
}